package main

import (
	"context"
	"fmt"
	"os/exec"
	"time"
)

type result struct {
	output []byte
	err    error
}

func main() {
	resultChan := make(chan *result, 1000)

	// 超时强杀
	ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
	defer cancel() // 关闭ctx树下所有channel

	go func() {
		cmd := exec.CommandContext(ctx, `c:\cygwin64\bin\bash`, "sleep 2;ls -l")
		output, err := cmd.CombinedOutput()

		resultChan <- &result{
			output: output,
			err:    err,
		}
	}()

	for msg := range resultChan {
		fmt.Printf("msg:%+v", string(msg.output))
	}
}
